"""
@File    : redis_operate.py
@Time    : 2021/6/1 23:37
@Author  : chenhuayan
@Software: PyCharm
"""
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import redis
from config.setting import REDIS_HOST, REDIS_PORT, REDIS_PASSWD, EXPIRE_TIME


class RedisDd():
    def __init__(self, host, port, passwd):
        # 建立数据库连接
        self.r = redis.Redis(
            host=host,
            port=port,
            password=passwd,
            # 得到字符串类型的数据
            decode_responses=True
        )

    def handle_redis_token(self, key, value=None):
        # 如果value非空，那么就设置key和value，EXPIRE_TIME为过期时间
        if value:
            self.r.set(key, value, ex=EXPIRE_TIME)
        # 如果value为空，那么直接通过key从redis中取值
        else:
            redis_tokan = self.r.get(key)
            return redis_tokan


redis_db = RedisDd(REDIS_HOST, REDIS_PORT, REDIS_PASSWD)
